/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.util
{
	public class HTMLUtil
	{
		/**
		 * removes all HTML tags from given string but not allowed one
		 * @param html
		 * @param tags
		 * @see http://www.flexer.info/2008/04/08/strip-html-tags-with-allowable-tags/
		 */
		public static function stripHtmlTags(html:String, tags:String = ""):String
		{
		    var tagsToBeKept:Array = new Array();
		    if (tags.length > 0)
		        tagsToBeKept = tags.split(new RegExp("\\s*,\\s*"));
		 
		    var tagsToKeep:Array = new Array();
		    for (var i:int = 0; i < tagsToBeKept.length; i++)
		    {
		        if (tagsToBeKept[i] != null && tagsToBeKept[i] != "")
		            tagsToKeep.push(tagsToBeKept[i]);
		    }
		 
		    var toBeRemoved:Array = new Array();
		    var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");
		 
		    var foundedStrings:Array = html.match(tagRegExp);
		    for (i = 0; i < foundedStrings.length; i++) 
		    {
		        var tagFlag:Boolean = false;
		        if (tagsToKeep != null) 
		        {
		            for (var j:int = 0; j < tagsToKeep.length; j++)
		            {
		                var tmpRegExp:RegExp = new RegExp("<\/?" + tagsToKeep[j] + "[^<>]*?>", "i");
		                var tmpStr:String = foundedStrings[i] as String;
		                if (tmpStr.search(tmpRegExp) != -1) 
		                    tagFlag = true;
		            }
		        }
		        if (!tagFlag)
		            toBeRemoved.push(foundedStrings[i]);
		    }
		    for (i = 0; i < toBeRemoved.length; i++) 
		    {
		        var tmpRE:RegExp = new RegExp("([\+\*\$\/])","g");
		        var tmpRemRE:RegExp = new RegExp((toBeRemoved[i] as String).replace(tmpRE, "\\$1"),"g");
		        html = html.replace(tmpRemRE, "");
		    } 
		    return html;
		}
		
		public static function removeHref(html:String):String
		{
			var reg:RegExp = new RegExp("(?=<a href).*(?<=</a>)" ,"gi");
			return html.replace(reg, "");
		}
		
		public static function entitiesConvert(str:String):String
		{
			var aryEntities:Object = new Object();
			aryEntities["&nbsp;"] = "\u00A0"; // non-breaking space
			aryEntities["&#160;"] = "\u00A0"; // non-breaking space
			
			aryEntities["&iexcl;"] = "\u00A1"; // inverted exclamation mark
			aryEntities["&162;"] = "\u00A1"; // inverted exclamation mark
			
			aryEntities["&cent;"] = "\u00A2"; // cent sign
			aryEntities["&162;"] = "\u00A2"; // cent sign
			
			
			aryEntities["&pound;"] = "\u00A3"; // pound sign
			aryEntities["&163;"] = "\u00A3"; // pound sign
			
			
			aryEntities["&curren;"] = "\u00A4"; // currency sign
			aryEntities["&164;"] = "\u00A4"; // currency sign
			
			
			aryEntities["&yen;"] = "\u00A5"; // yen sign
			aryEntities["&165;"] = "\u00A5"; // yen sign
			
			
			aryEntities["&brvbar;"] = "\u00A6"; // broken vertical bar (|)
			aryEntities["&166;"] = "\u00A6"; // broken vertical bar (|)
			
			
			aryEntities["&sect;"] = "\u00A7"; // section sign
			aryEntities["&167;"] = "\u00A7"; // section sign
			
			aryEntities["&uml;"] = "\u00A8"; // diaeresis
			aryEntities["&168;"] = "\u00A8"; // diaeresis
			
			aryEntities["&copy;"] = "\u00A9"; // copyright sign
			aryEntities["&169;"] = "\u00A9"; // copyright sign
			
			aryEntities["&reg;"] = "\u00AE"; // registered sign
			aryEntities["&174;"] = "\u00AE"; // registered sign
			
			aryEntities["&deg;"] = "\u00B0"; // degree sign
			aryEntities["&174;"] = "\u00B0"; // degree sign
			
			aryEntities["&plusmn;"] = "\u00B1"; // plus-minus sign
			aryEntities["&177;"] = "\u00B1"; // plus-minus sign
			
			// remove spaces from next three lines in actual code
			aryEntities["&sup1;"] = "\u00B9"; // superscript one
			//aryEntities["&174;"] = "\u00B9"; // superscript one
			
			aryEntities["&sup2;"] = "\u00B2"; // superscript two
			aryEntities["&178;"] = "\u00B2"; // superscript two
			
			aryEntities["&sup3;"] = "\u00B3"; // superscript three
			aryEntities["&179;"] = "\u00B3"; // superscript three
			
			aryEntities["&acute;"] = "\u00B4"; // acute accent
			aryEntities["&180;"] = "\u00B4"; // acute accent
			
			aryEntities["&micro;"] = "\u00B5"; // micro sign
			aryEntities["&181;"] = "\u00B5"; // micro sign
			
			// remove spaces from next three lines in actual code
			aryEntities["&frac14;"] = "\u00BC"; // vulgar fraction one quarter
			aryEntities["&188;"] = "\u00BC"; // vulgar fraction one quarter
			
			aryEntities["&frac12;"] = "\u00BD"; // vulgar fraction one half
			aryEntities["&189;"] = "\u00BD"; // vulgar fraction one half
			
			aryEntities["&frac34;"] = "\u00BE"; // vulgar fraction three quarters
			aryEntities["&190;"] = "\u00BE"; // vulgar fraction three quarters
			
			aryEntities["&iquest;"] = "\u00BF"; // inverted question mark
			aryEntities["&191;"] = "\u00BF"; // inverted question mark
			
			aryEntities["&Agrave;"] = "\u00C0"; // Latin capital letter A with grave
			aryEntities["&192;"] = "\u00C0"; // Latin capital letter A with grave
			
			aryEntities["&Aacute;"] = "\u00C1"; // Latin capital letter A with acute
			aryEntities["&193;"] = "\u00C1"; // Latin capital letter A with acute
			
			aryEntities["&Acirc;"] = "\u00C2"; // Latin capital letter A with circumflex
			aryEntities["&194;"] = "\u00C2"; // Latin capital letter A with circumflex
			
			aryEntities["&Atilde;"] = "\u00C3"; // Latin capital letter A with tilde
			aryEntities["&195;"] = "\u00C3"; // Latin capital letter A with tilde
			
			aryEntities["&Auml;"] = "\u00C4"; // Latin capital letter A with diaeresis
			aryEntities["&196;"] = "\u00C4"; // Latin capital letter A with diaeresis
			
			aryEntities["&Aring;"] = "\u00C5"; // Latin capital letter A with ring above
			aryEntities["&197;"] = "\u00C5"; // Latin capital letter A with ring above
			
			aryEntities["&AElig;"] = "\u00C6"; // Latin capital letter AE
			aryEntities["&198;"] = "\u00C6"; // Latin capital letter AE
			
			aryEntities["&Ccedil;"] = "\u00C7"; // Latin capital letter C with cedilla
			aryEntities["&199;"] = "\u00C7"; // Latin capital letter C with cedilla
			
			aryEntities["&Egrave;"] = "\u00C8"; // Latin capital letter E with grave
			aryEntities["&200;"] = "\u00C8"; // Latin capital letter E with grave
			
			aryEntities["&Eacute;"] = "\u00C9"; // Latin capital letter E with acute
			aryEntities["&201;"] = "\u00C9"; // Latin capital letter E with acute
			
			aryEntities["&Ecirc;"] = "\u00CA"; // Latin capital letter E with circumflex
			aryEntities["&202;"] = "\u00CA"; // Latin capital letter E with circumflex
			
			aryEntities["&Euml;"] = "\u00CB"; // Latin capital letter E with diaeresis
			aryEntities["&203;"] = "\u00CB"; // Latin capital letter E with diaeresis
			
			aryEntities["&Igrave;"] = "\u00CC"; // Latin capital letter I with grave
			aryEntities["&204;"] = "\u00CC"; // Latin capital letter I with grave
			
			aryEntities["&Iacute;"] = "\u00CD"; // Latin capital letter I with acute
			aryEntities["&205;"] = "\u00CD"; // Latin capital letter I with acute
			
			aryEntities["&Icirc;"] = "\u00CE"; // Latin capital letter I with circumflex
			aryEntities["&206;"] = "\u00CE"; // Latin capital letter I with circumflex
			
			aryEntities["&Iuml;"] = "\u00CF"; // Latin capital letter I with diaeresis
			aryEntities["&207;"] = "\u00CF"; // Latin capital letter I with diaeresis
			
			aryEntities["&ETH;"] = "\u00D0"; // Latin capital letter ETH
			aryEntities["&208;"] = "\u00D0"; // Latin capital letter ETH
			
			aryEntities["&Ntilde;"] = "\u00D1"; // Latin capital letter N with tilde
			aryEntities["&209;"] = "\u00D1"; // Latin capital letter N with tilde
			
			aryEntities["&Ograve;"] = "\u00D2"; // Latin capital letter O with grave
			aryEntities["&210;"] = "\u00D2"; // Latin capital letter O with grave
			
			aryEntities["&Oacute;"] = "\u00D3"; // Latin capital letter O with acute
			aryEntities["&211;"] = "\u00D3"; // Latin capital letter O with acute
			
			aryEntities["&Ocirc;"] = "\u00D4"; // Latin capital letter O with circumflex
			aryEntities["&212;"] = "\u00D4"; // Latin capital letter O with circumflex
			
			aryEntities["&Otilde;"] = "\u00D5"; // Latin capital letter O with tilde
			aryEntities["&213;"] = "\u00D5"; // Latin capital letter O with tilde
			
			aryEntities["&Ouml;"] = "\u00D6"; // Latin capital letter O with diaeresis
			aryEntities["&214;"] = "\u00D6"; // Latin capital letter O with diaeresis
			
			aryEntities["&Oslash;"] = "\u00D8"; // Latin capital letter O with stroke
			aryEntities["&216;"] = "\u00D8"; // Latin capital letter O with stroke
			
			aryEntities["&Ugrave;"] = "\u00D9"; // Latin capital letter U with grave
			aryEntities["&217;"] = "\u00D9"; // Latin capital letter U with grave
			
			aryEntities["&Uacute;"] = "\u00DA"; // Latin capital letter U with acute
			aryEntities["&218;"] = "\u00DA"; // Latin capital letter U with acute
			
			aryEntities["&Ucirc;"] = "\u00DB"; // Latin capital letter U with circumflex
			aryEntities["&219;"] = "\u00DB"; // Latin capital letter U with circumflex
			
			aryEntities["&Uuml;"] = "\u00DC"; // Latin capital letter U with diaeresis
			aryEntities["&220;"] = "\u00DC"; // Latin capital letter U with diaeresis
			
			aryEntities["&Yacute;"] = "\u00DD"; // Latin capital letter Y with acute
			aryEntities["&221;"] = "\u00DD"; // Latin capital letter Y with acute
			
			aryEntities["&THORN;"] = "\u00DE"; // Latin capital letter THORN
			aryEntities["&222;"] = "\u00DE"; // Latin capital letter THORN
			
			aryEntities["&szlig;"] = "\u00DF"; // Latin small letter sharp s = ess-zed
			aryEntities["&223;"] = "\u00DF"; // Latin small letter sharp s = ess-zed
			
			aryEntities["&agrave;"] = "\u00E0"; // Latin small letter a with grave
			aryEntities["&224;"] = "\u00E0"; // Latin small letter a with grave
			
			aryEntities["&aacute;"] = "\u00E1"; // Latin small letter a with acute
			aryEntities["&225;"] = "\u00E1"; // Latin small letter a with acute
			
			aryEntities["&acirc;"] = "\u00E2"; // Latin small letter a with circumflex
			aryEntities["&226;"] = "\u00E2"; // Latin small letter a with circumflex
			
			aryEntities["&atilde;"] = "\u00E3"; // Latin small letter a with tilde
			aryEntities["&227;"] = "\u00E3"; // Latin small letter a with tilde
			
			aryEntities["&auml;"] = "\u00E4"; // Latin small letter a with diaeresis
			aryEntities["&228;"] = "\u00E4"; // Latin small letter a with diaeresis
			
			aryEntities["&aring;"] = "\u00E5"; // Latin small letter a with ring above
			aryEntities["&229;"] = "\u00E5"; // Latin small letter a with ring above
			
			aryEntities["&aelig;"] = "\u00E6"; // Latin small letter ae
			aryEntities["&230;"] = "\u00E6"; // Latin small letter ae
			
			aryEntities["&ccedil;"] = "\u00E7"; // Latin small letter c with cedilla
			aryEntities["&231;"] = "\u00E7"; // Latin small letter c with cedilla
			
			aryEntities["&egrave;"] = "\u00E8"; // Latin small letter e with grave
			aryEntities["&232;"] = "\u00E8"; // Latin small letter e with grave
			
			aryEntities["&eacute;"] = "\u00E9"; // Latin small letter e with acute
			aryEntities["&233;"] = "\u00E9"; // Latin small letter e with acute
			
			aryEntities["&ecirc;"] = "\u00EA"; // Latin small letter e with circumflex
			aryEntities["&234;"] = "\u00EA"; // Latin small letter e with circumflex
			
			aryEntities["&euml;"] = "\u00EB"; // Latin small letter e with diaeresis
			aryEntities["&235;"] = "\u00EB"; // Latin small letter e with diaeresis
			
			aryEntities["&igrave;"] = "\u00EC"; // Latin small letter i with grave
			aryEntities["&236;"] = "\u00EC"; // Latin small letter i with grave
			
			aryEntities["&iacute;"] = "\u00ED"; // Latin small letter i with acute
			aryEntities["&237;"] = "\u00ED"; // Latin small letter i with acute
			
			aryEntities["&icirc;"] = "\u00EE"; // Latin small letter i with circumflex
			aryEntities["&238;"] = "\u00EE"; // Latin small letter i with circumflex
			
			aryEntities["&iuml;"] = "\u00EF"; // Latin small letter i with diaeresis
			aryEntities["&239;"] = "\u00EF"; // Latin small letter i with diaeresis
			
			aryEntities["&eth;"] = "\u00F0"; // Latin small letter eth
			aryEntities["&240;"] = "\u00F0"; // Latin small letter eth
			
			aryEntities["&ntilde;"] = "\u00F1"; // Latin small letter n with tilde
			aryEntities["&241;"] = "\u00F1"; // Latin small letter n with tilde
			
			aryEntities["&ograve;"] = "\u00F2"; // Latin small letter o with grave
			aryEntities["&242;"] = "\u00F2"; // Latin small letter o with grave
			
			aryEntities["&oacute;"] = "\u00F3"; // Latin small letter o with acute
			aryEntities["&243;"] = "\u00F3"; // Latin small letter o with acute
			
			aryEntities["&ocirc;"] = "\u00F4"; // Latin small letter o with circumflex
			aryEntities["&244;"] = "\u00F4"; // Latin small letter o with circumflex
			
			aryEntities["&otilde;"] = "\u00F5"; // Latin small letter o with tilde
			aryEntities["&245;"] = "\u00F5"; // Latin small letter o with tilde
			
			aryEntities["&ouml;"] = "\u00F6"; // Latin small letter o with diaeresis
			aryEntities["&246;"] = "\u00F6"; // Latin small letter o with diaeresis
			
			aryEntities["&oslash;"] = "\u00F8"; // Latin small letter o with stroke
			aryEntities["&248;"] = "\u00F8"; // Latin small letter o with stroke
			
			aryEntities["&ugrave;"] = "\u00F9"; // Latin small letter u with grave
			aryEntities["&249;"] = "\u00F9"; // Latin small letter u with grave
			
			aryEntities["&uacute;"] = "\u00FA"; // Latin small letter u with acute
			aryEntities["&250;"] = "\u00FA"; // Latin small letter u with acute
			
			aryEntities["&ucirc;"] = "\u00FB"; // Latin small letter u with circumflex
			aryEntities["&251;"] = "\u00FB"; // Latin small letter u with circumflex
			
			aryEntities["&uuml;"] = "\u00FC"; // Latin small letter u with diaeresis
			aryEntities["&252;"] = "\u00FC"; // Latin small letter u with diaeresis
			
			aryEntities["&yacute;"] = "\u00FD"; // Latin small letter y with acute
			aryEntities["&253;"] = "\u00FD"; // Latin small letter y with acute
			
			aryEntities["&thorn;"] = "\u00FE"; // Latin small letter thorn
			aryEntities["&254;"] = "\u00FE"; // Latin small letter thorn
			
			aryEntities["&yuml;"] = "\u00FF"; // Latin small letter y with diaeresis
			aryEntities["&255;"] = "\u00FF"; // Latin small letter y with diaeresis
			
			//return aryEntities;
			
			
			
			for (var entity:String in aryEntities)
			{
				str = str.split(entity).join(aryEntities[entity]);
			}
			
			
			return str;
			
		}

	}
}